package ssm.service;

import org.apache.taglibs.standard.lang.jstl.NullLiteral;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import ssm.dao.MyResult;
import ssm.dao.OpenedState;
import ssm.dao.Teacher;
import ssm.dao.TeacherCheckWorkAll;
import ssm.model.*;
import ssm.utils.Const;
import ssm.utils.TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class PerceptionServiceImpl implements  PerceptionService{
    public  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TrackMapper trackMapper;
    @Autowired
    private SnapMapper snapMapper;
    @Autowired
    private TeacherReachLeaveMapper teacherReachLeaveMapper ;
    @Autowired
    private TeacherCheckWorkMapper teacherCheckWorkMapper;
    @Autowired
    private TeacherCheckWorkAllMapper teacherCheckWorkAllMapper;
    @Autowired
    private TrackPerceptionMapper trackPerceptionMapper;


    @Value(value = "${zscd}")
    private String zscd;
    @Value(value = "${wsjb}")
    private String wsjb;

    //主--获取在线人数以及班级和部门树状
    public Map<String,Object> getnumberAndTree(String time) {
//        System.out.println("zscd:"+zscd);
        Map<String,Object> result=new HashMap<String,Object>();
        //获取学生、教师感知(在校)数据
        Long time0=System.currentTimeMillis();
        Map<String,Object> number=getNumber(time);
        //获取学生(班级)、教师(部门)树状数据
        Map<String,Object> tree=getTree(time);
        result.put("number",number);
        result.put("tree",tree);
        return result;
    }
    //主--在校日历数据
    public Map<String,Map<String,Object>> getInSchoolTable(String gh) throws ParseException {
        String [] months= TimeUtils.getLast6Months();
        System.out.println(Arrays.toString(months));
        //教师在校感知数据获取
        List<String> ganzhiData=trackMapper.getDates(gh);
        //获取教师迟到数据
       List<String> chidaoData=teacherReachLeaveMapper.getChiDaoDates(gh,zscd);
        //获取教师加班数据
        List<String> jiabanData=teacherReachLeaveMapper.getJiaBanDates(gh,wsjb);
        //获取教师请假数据
        List<String> qingjiaData=teacherCheckWorkMapper.getQingJiaDatas(gh);
        //获取教师外出访学数据
        List<String> fangxueData=teacherCheckWorkMapper.getFangXueDatas(gh);

        Map<String,Map<String,Object>>result=new LinkedHashMap<String,Map<String,Object>>();
        Map<String,Object> ganzhi=new LinkedHashMap<String, Object>();
        Map<String,Object> chidao=new LinkedHashMap<String, Object>();
        Map<String,Object> jiaban=new LinkedHashMap<String, Object>();
        Map<String,Object> qingjia=new LinkedHashMap<String, Object>();
        Map<String,Object> fangxue=new LinkedHashMap<String, Object>();
        for(String month:months){
            List<String>ganzhi_list=new ArrayList<String>();
            List<String>chidao_list=new ArrayList<String>();
            List<String>jiaban_list=new ArrayList<String>();
            List<String>qingjia_list=new ArrayList<String>();
            List<String>fangxue_list=new ArrayList<String>();
            //教师在校感知数据封装
            if(ganzhiData.size()==0){
                ganzhi.put(month,ganzhi_list);
            }else{
                for(String date:ganzhiData){
                    if(month.equals(date.substring(0,7))){
                        ganzhi_list.add(date);
                    }
                }
                System.out.println(month);
                ganzhi.put(month,ganzhi_list);
            }
            //教师迟到数据封装
            if(chidaoData.size()==0){
                chidao.put(month,chidao_list);
            }else{
                for(String date:chidaoData){
                    if(month.equals(date.substring(0,7))){
                        if(!TimeUtils.isZhoumo(date)){
                            chidao_list.add(date);
                        }
                    }
                }
                System.out.println(month);
                chidao.put(month,chidao_list);
            }
            //教师晚上加班数据封装
            if(jiabanData.size()==0){
                jiaban.put(month,jiaban_list);
            }else{
                for(String date:jiabanData){
                    if(month.equals(date.substring(0,7))){
                            jiaban_list.add(date);
                    }
                }
                System.out.println(month);
                jiaban .put(month,jiaban_list);
            }
            //教师周末加班数据

            //教师请假数据封装
            if(qingjiaData.size()==0){
                qingjia.put(month,qingjia_list);
            }else{
                for(String date:qingjiaData){
                    if(month.equals(date.substring(0,7))){
                            qingjia_list.add(date);
                    }
                }
                System.out.println(month);
                qingjia .put(month,qingjia_list);
            }

            //教师外出数据封装
            if(fangxueData.size()==0){
                fangxue.put(month,fangxue_list);
            }else{
                for(String date:fangxueData){
                    if(month.equals(date.substring(0,7))){
                        fangxue_list.add(date);
                    }
                }
                System.out.println(month);
                fangxue .put(month,fangxue_list);
            }
        }


        result.put("ganzhi",ganzhi);
        result.put("chidao",chidao);
        result.put("jiaban",jiaban);
        result.put("qingjia",qingjia);
        result.put("fangxue",fangxue);
        return result;
    }
    //主--班级在校情况
    public List<Map<String, Object>> getInSchoolByClass(List<String> list, String time) {

        List<Map<String, Object>> result=new ArrayList<Map<String, Object>>();
        List<Integer> years=getYears(time);
        String startTime=time+" 00:00:00";
        String endTime=time+" 23:59:59";
        if (null==list||list.isEmpty()||"".equals(list.toString())){
            list=studentMapper.getAllZY(years);
        }

        Map<String,Object> paramBySum=new HashMap<String,Object>();
        paramBySum.put("list",list);
        paramBySum.put("years",years);
        //获取所有班级总人数
        List<Map<String,String>> classSum=studentMapper.getBjAndSum(paramBySum);
        Map<String,Object> paramByNum=new HashMap<String,Object>();
        paramByNum.put("list",list);
        paramByNum.put("startTime",startTime);
        paramByNum.put("endTime",endTime);
        //获取所有班级在校人数
        List<Map<String,String>> classNum=trackMapper.getBjAndNum(paramByNum);
        //System.out.println(list);
        System.out.println("classSum:-------"+classSum);
        System.out.println("classNum:-------"+classNum);
        for(Map<String,String> sum:classSum){
            Map<String,Object> classMap=new HashMap<String, Object>();
            classMap.putAll(sum);
            for(Map<String,String>num:classNum){
                if(sum.get("bj").equals(num.get("bj"))&&sum.get("zy").equals(num.get("zy"))){
                    //System.out.println();
                    classMap.put("num",num.get("num"));
                    break;
                }
            }
            if(!classMap.containsKey("num")){
                classMap.put("num",0);
            }
        result.add(classMap);
        }

        return result;
    }
    //主--教师在校情况
    public Map<String, Object> getInSchoolByTeacher(List<String> list, String time) {
        if(null==list||list.isEmpty()||"".equals(list.toString())){
            list=teacherMapper.getAllDw();
        }
        Map<String,Object> result=new LinkedHashMap<String, Object>();
        String startTime=time+" 00:00:00";
        String endTime=time+" 23:59:59";
        //获取教师全数据
        List<Map<String,Object>> teachMess=teacherMapper.getMess(list);
        //获取在校教师工号集合
        List<String> inSchoolByGh=trackMapper.getInSchoolByGh(startTime,endTime);
        //todo 2018-03-28需要新加在校异常与离校异常
        Map<String, List<String>> except = getInOrOutSchoolByExcept(time);
        System.out.println(list);
        for(String dw:list){
            List<Map<String,Object>> dwMess=new ArrayList<Map<String, Object>>();
            for(Map<String,Object>map:teachMess){
                if(dw.equals(map.get("dw"))){
                    //System.out.println(1);
                    Map<String,Object> mapdw=new HashMap<String,Object>();
                    mapdw.put("xm",map.get("xm"));
                    mapdw.put("gh",map.get("gh"));
                    if(inSchoolByGh.contains(map.get("gh"))){
                        mapdw.put("sfzx",1);
                    }else{
                        mapdw.put("sfzx",0);
                    }
                    if(except!=null&&!except.isEmpty()){
                        mapdw=addException(mapdw,except,map);
                    }else{
                        mapdw=addNullException(mapdw);
                    }
                    dwMess.add(mapdw);
                }
            }
            if(null!=dwMess&&!dwMess.isEmpty()){
                result.put(dw,dwMess);
            }
        }

        return result;
    }

    private Map<String,Object>addNullException(Map<String,Object>mapdw){
        mapdw.put("zx_morning",0);
        mapdw.put("zx_afternoon",0);
        mapdw.put("zx_evening",0);
        mapdw.put("zx_midnight",0);
        mapdw.put("lx_moring",0);
        mapdw.put("lx_afternoon",0);
        mapdw.put("lx_evening",0);
        mapdw.put("lx_midnight",0);
        return mapdw;
    }

    private Map<String,Object> addException(Map<String,Object>mapdw,Map<String, List<String>> except, Map<String, Object> map) {
        if(except.get("zx_morning")!=null&&except.get("zx_morning").contains(map.get("gh"))){
            mapdw.put("zx_morning",1);
        }else{
            mapdw.put("zx_morning",0);
        }
        if(except.get("zx_afternoon")!=null&&except.get("zx_afternoon").contains(map.get("gh"))){
            mapdw.put("zx_afternoon",1);
        }else{
            mapdw.put("zx_afternoon",0);
        }
        if(except.get("zx_evening")!=null&&except.get("zx_evening").contains(map.get("gh"))){
            mapdw.put("zx_evening",1);
        }else{
            mapdw.put("zx_evening",0);
        }
        if(except.get("zx_midnight")!=null&&except.get("zx_midnight").contains(map.get("gh"))){
            mapdw.put("zx_midnight",1);
        }else{
            mapdw.put("zx_midnight",0);
        }
        if(except.get("lx_moring")!=null&&except.get("lx_moring").contains(map.get("gh"))){
            mapdw.put("lx_moring",1);
        }else{
            mapdw.put("lx_moring",0);
        }
        if(except.get("lx_afternoon")!=null&&except.get("lx_afternoon").contains(map.get("gh"))){
            mapdw.put("lx_afternoon",1);
        }else{
            mapdw.put("lx_afternoon",0);
        }
        if(except.get("lx_evening")!=null&&except.get("lx_evening").contains(map.get("gh"))){
            mapdw.put("lx_evening",1);
        }else{
            mapdw.put("lx_evening",0);
        }
        if(except.get("lx_midnight")!=null&&except.get("lx_midnight").contains(map.get("gh"))){
            mapdw.put("lx_midnight",1);
        }else{
            mapdw.put("lx_midnight",0);
        }
        return mapdw;
    }

    //主--学生在校情况
    public List<Map<String,Object>> getInSchoolByStudent(String zy,String bj, String time) {
        String startTime=time+" 00:00:00";
        String endTime=time+" 23:59:59";
        //获取该班级全部学生
        List<Map<String,Object>> stuMess=studentMapper.getStuMess(zy,bj);
        //获取在校学生
        List<String> inSchoolByXh=trackMapper.getInschoolByXh(startTime,endTime);
        //封装
        List<Map<String,Object>>result=new ArrayList<Map<String, Object>>();
        for(Map<String,Object> stu:stuMess){
            Map<String,Object> student=new HashMap<String,Object>();
            student.putAll(stu);
            if(inSchoolByXh.contains(stu.get("xh"))){
                student.put("sfzx",1);
            }else{
                student.put("sfzx",0);
            }
            result.add(student);

        }
        return result;
    }

    public Map<String,Object> getOutSchoolPersonCount(String time) {
        String starttime=time+" 00:00:00";
        String endtime=time+" 23:59:59";
        Map<String,Object>param=new HashMap<String,Object>();
        param.put("starttime",starttime);
        param.put("endtime",endtime);
        int resultCount=snapMapper.getOutSchoolPersonCount(param);
        Map<String,Object>result=new HashMap<String,Object>();
        result.put("count",resultCount);
        return result;
    }

    public Map<String, Object> getLast14DaysInfo(String time) throws ParseException {
        String[]days=TimeUtils.getLast14Days(time);
        Map<String,Object>result=new LinkedHashMap<String,Object>();


        for(int i=days.length-1;i>=0;i--){
            String date=days[i];
            Map<String,Object>oldResult=getCountOnOld(date);
            System.out.println(i+"："+oldResult);
            result.put(date,oldResult);
        }
        if(time.equals(sdf.format(new Date()))){
//            System.out.println("走今天");
            Map<String,Object> todayResult=getCountOnToday(time);
            result.put(time,todayResult);

        }else{
//            System.out.println("走旧数据    ");
            Map<String,Object> todayResult=getCountOnOld(time);
            result.put(time,todayResult);
        }
        return result;
    }



    //返回教师人员表(0-迟到 1-晚上加班 2-双休加班)
    public Map<String, Object> getTeacherStatusInfo(String date) throws ParseException {
        Map<String,Object> result=new LinkedHashMap<String, Object>();
        //获取所有单位
        List<String>list=teacherMapper.getAllDw();
        //teacherResult为所有满足条件老师信息---主要xm,xhgh,dw
        List<Map> cdResult=new ArrayList<Map>();
        List<Map> wsjbResult=new ArrayList<Map>();
        List<Map> zmjbResult=new ArrayList<Map>();
        //构造晚上加班变量
        String cdsjbz=date+" "+zscd;
        String wsjbsjbz=date+" "+wsjb;
        //表示为今天数据,去track中查询
        if(sdf.format(new Date()).equals(date)){
            zmjbResult=trackMapper.getZmsbTeacher(date);
            cdResult=trackMapper.getCdTeacher(date,cdsjbz);
            wsjbResult=trackMapper.getWsjbTeacher(date,wsjbsjbz);
        }else{
            zmjbResult=teacherReachLeaveMapper.getZmsbTeacher(date);
            cdResult=teacherReachLeaveMapper.getCdTeacher(date,cdsjbz);
            wsjbResult=teacherReachLeaveMapper.getWsjbTeacher(date,wsjbsjbz);
        }
        //获取教师全数据
        List<Map<String,Object>> teachMess=teacherMapper.getMess(list);
        //对满足条件教师进行统一处理
        for(String dw:list){
            List<Map<String,Object>> dwMess=new ArrayList<Map<String, Object>>();
            for(Map<String,Object>teachermap:teachMess){
                if(dw.equals(teachermap.get("dw"))){
                    //System.out.println(1);
                    Map<String,Object> mapdw=new HashMap<String,Object>();
                    mapdw.put("xm",teachermap.get("xm"));
                    mapdw.put("gh",teachermap.get("gh"));
                    //周末加班信息处理
                    if(TimeUtils.isZhoumo(date)){
                        if(!zmjbResult.isEmpty()){
                            for(Map<String,Object> re:zmjbResult){
                                if(teachermap.get("gh").equals(re.get("xhgh"))){
                                    mapdw.put("zmjb",1);
                                    break;
                                }
                            }
//                        mapdw.put("zmjb",0);
                        }else{
                            System.out.println("zmjb为空");
                            mapdw.put("zmjb",0);
                        }
                       mapdw.put("cd",0);
                        mapdw.put("wsjb",0);
                    }else{
                        mapdw.put("zmjb",0);
                        //迟到信息处理
                        boolean cdflag=true;
                        boolean wsjbflag=true;
                        if(!cdResult.isEmpty()){
                            for(Map<String,Object> re:cdResult){
                                if(teachermap.get("gh").equals(re.get("xhgh"))){
                                    mapdw.put("cd",1);
                                    cdflag=false;
                                    break;
                                }
                            }
                        }else{
                            System.out.println("zmjb为空");
                            mapdw.put("cd",0);
                        }
                        //晚上加班信息处理
                        if(!wsjbResult.isEmpty()){
                            for(Map<String,Object> re:wsjbResult){
                                if(teachermap.get("gh").equals(re.get("xhgh"))){
                                    mapdw.put("wsjb",1);
                                    wsjbflag=false;
                                    break;
                                }
                            }
                        }else{
                            mapdw.put("wsjb",0);
                        }

                        if(cdflag){
                            mapdw.put("cd",0);
                        }
                        if(wsjbflag){
                            mapdw.put("wsjb",0);
                        }
                    }
                    dwMess.add(mapdw);
                }
            }

            if(null!=dwMess&&!dwMess.isEmpty()){
                result.put(dw,dwMess);
            }
        }
        System.out.println("cd.size():"+cdResult.size());
        return result;
    }

    public Map<String,Object> getCountOnOld(String date) throws ParseException {
        int cdCount=0;
        int wsjbCount=0;
        int zmjbCount=0;
        Map<String,Object>result=new HashMap<String,Object>();
        String starttime=date+" 00:00:00";
        String endtime=date+" 23:59:59";
        String dxsjbz=date+" 08:30:00";
        String wsjbbz=date+" 21:00:00";
        //todo  修改
        List<Map>resultList=teacherReachLeaveMapper.getCountOnOld(date);
        if(TimeUtils.isZhoumo(date)){
            zmjbCount=resultList.size();
            result.put("zmjbCount",zmjbCount);
        }else{
            for(Map<String,Object>map:resultList){
                String dxsj=(String)map.get("dxsj");
                String lxsj=(String)map.get("lxsj");
                if(TimeUtils.compare_date_1big2(dxsj,dxsjbz)){
                    //迟到
                    cdCount++;
                }
                if(TimeUtils.compare_date_1big2(lxsj,wsjbbz)){
                    //晚上加班
                    wsjbCount++;
                }
                result.put("cdCount",cdCount);
                result.put("wsjbCount",wsjbCount);
            }

        }
        System.out.println(date+":"+"cdCount>>"+cdCount+",wsjbCount"+wsjbCount+",zmjbCount》》"+zmjbCount);
        return  result;

    }

    public Map<String,Object> getCountOnToday(String date) throws ParseException {
        int cdCount=0;
        int wsjbCount=0;
        int zmjbCount=0;
        Map<String,Object>result=new HashMap<String,Object>();
        String starttime=date+" 00:00:00";
        String endtime=date+" 23:59:59";
        String dxsjbz=date+" 08:30:00";
        String wsjbbz=date+" 21:00:00";
        List<Map>resultList=trackMapper.getCountOnToday(starttime,endtime);
        if(TimeUtils.isZhoumo(date)){
            zmjbCount=resultList.size();
            result.put("zmjbCount",zmjbCount);
        }else{
            for(Map<String,Object>map:resultList){
                String dxsj=(String)map.get("dxsj");
                String lxsj=(String)map.get("lxsj");
                if(TimeUtils.compare_date_1big2(dxsj,dxsjbz)){
                    //迟到
                    cdCount++;
                }
                if(TimeUtils.compare_date_1big2(lxsj,wsjbbz)){
                    //晚上加班
                    wsjbCount++;
                }
                result.put("cdCount",cdCount);
                result.put("wsjbCount",wsjbCount);
            }

        }
//        System.out.println(date+":"+"cdCount>>"+cdCount+",wsjbCount"+wsjbCount+",zmjbCount》》"+zmjbCount);
        return  result;
    }



    private Map<String,Object> getTree(String time){
        Map<String,Object>result=new HashMap<String,Object>();

        List<Map>claseTree=getClassTree(time);
        List<Map>unitTree=getUnitTree();

        result.put("student",claseTree);
        result.put("teacher",unitTree);
        return result;
    }

    /**
     * 获取教师-部门树结构数据
     * @param
     * @return
     */
    private List<Map> getUnitTree() {

        List<Map> result=new ArrayList<Map>();
        Map<String,Object> xxMap=new HashMap<String,Object>();
        int dwIndex=1;
        //树中加入"警察学院"最高级目录
        xxMap.put("id",0+"");
        xxMap.put("text", "浙江警察学院");
        xxMap.put("parent", "#");
        xxMap.put("state", new OpenedState(false));
        xxMap.put("type", "star");
        result.add(xxMap);
        //遍历加入单位信息
        List<String> dws=teacherMapper.getDws();
        for (String dw:dws){
            Map<String,Object> dwMap=new HashMap<String,Object>();
            dwMap.put("id",dwIndex+"");
            dwMap.put("text", dw);
            dwMap.put("parent", "0");
            dwMap.put("state", new OpenedState(false));
            dwMap.put("type", "cloud");
            result.add(dwMap);
            dwIndex++;
        }

        return result;
    }

    /**
     * 获取学生-班级树结构数据
     * @param time
     * @return
     */
    private List<Map> getClassTree(String time) {
        List<Map> result=new ArrayList<Map>();
        List<Integer>years=getYears(time);
        List<String> xys=studentMapper.getXys(years);
        int xyInder=1;
        int zyInder=100;
        //树中加入"警察学院"最高级目录
        Map<String,Object> xxMap=new HashMap<String,Object>();
        xxMap.put("id",0+"");
        xxMap.put("text", "浙江警察学院");
        xxMap.put("parent", "#");
        xxMap.put("state", new OpenedState(false));
        xxMap.put("type", "star");
        result.add(xxMap);
        //遍历循环加入"分院"目录、"专业"最底层
        for (String xy:xys) {
            Map<String, Object> xyMap = new HashMap<String, Object>();
            xyMap.put("id",xyInder+"");
            xyMap.put("text", xy);
            xyMap.put("parent", "0");
            xyMap.put("state", new OpenedState(false));
            xyMap.put("type", "star");
            result.add(xyMap);
            Map<String,Object>param=new HashMap<String,Object>();


            param.put("xy",xy);
            param.put("list",years);
            List<String>zys=studentMapper.getZys(param);
            //System.out.println(zys);
            for(String zy:zys){
                Map<String, Object> zzMap = new HashMap<String, Object>();
                zzMap.put("id",zyInder+"");
                zzMap.put("text", zy);
                zzMap.put("parent",xyInder+"");
                zzMap.put("state", new OpenedState(false));
                zzMap.put("type", "cloud");
                result.add(zzMap);
                zyInder++;
            }
            xyInder++;
        }
        return result;
    }

    //处理num数据
    public Map<String,Object> getNumber(String time){
        Map<String,Object> result=new HashMap<String,Object>();
        String startTime=time+" 00:00:00";
        String endTime=time+" 23:59:59";
        List<Integer> years=getYears(time);
        //教师总数
        Long time1=System.currentTimeMillis();
        int teachSum=teacherMapper.getSum();
        System.out.println("总数:"+(System.currentTimeMillis()-time1));
        time1=System.currentTimeMillis();
        //教师感知人数
        int teachNum=trackMapper.getNumByTeacher(startTime,endTime);
        System.out.println("教师感知人数:"+(System.currentTimeMillis()-time1));
        time1=System.currentTimeMillis();
//        //教师实际感知人数,过滤掉mysql中teacher_check_work的allstatus=2,及外出请假人数
//        int realTeacherNum=trackMapper.getRealTrNumByTeacher(startTime,endTime,time);
        //todo  现在前端需求修改成,当天迟到,加班人员,外出访学,以及请假人员的显示

        Map<String,Object> cdAndJb=cdAndJbNum(time);
        System.out.println("获取迟到加班："+(System.currentTimeMillis()-time1));
        time1=System.currentTimeMillis();
        //国外访学
        int guowaifangxueNum=teacherCheckWorkMapper.getNum(time, Const.WorkStatus.GUOWAIFANGXUE.getCode());
        int guoneifangxueNum=teacherCheckWorkMapper.getNum(time,Const.WorkStatus.GUONEIFANGXUE.getCode());
        int peixunNum=teacherCheckWorkMapper.getNum(time,Const.WorkStatus.PEIXUN.getCode());
        int duboNum=teacherCheckWorkMapper.getNum(time,Const.WorkStatus.DUBO.getCode());
        int waijieNum=teacherCheckWorkMapper.getNum(time,Const.WorkStatus.WAIJIE.getCode());
        int duanlian=teacherCheckWorkMapper.getNum(time,Const.WorkStatus.DUANLIAN.getCode());
        //请假
        int shijiaNum=teacherCheckWorkMapper.getNum(time,Const.WorkStatus.SHIJIA.getCode());
        int bingjiaNum=teacherCheckWorkMapper.getNum(time,Const.WorkStatus.BINGJIA.getCode());
        System.out.println("获取其他标签人数:"+(System.currentTimeMillis()-time1));
        time1=System.currentTimeMillis();
        int studSum=studentMapper.getSum();
        int studNum=trackMapper.getNumByStudent(startTime,endTime);
        System.out.println("学生数据:"+(System.currentTimeMillis()-time1));
        time1=System.currentTimeMillis();
        Map<String,Object> teachers=new HashMap<String,Object>();
        teachers.put("num",teachNum);
        teachers.put("sum",teachSum);
        teachers.put("cd",cdAndJb.get("cdNum"));
        teachers.put("wsjb",cdAndJb.get("wsjbNum"));
        teachers.put("zmjb",cdAndJb.get("zmjbNum"));
        teachers.put("guowaifangxueNum",guowaifangxueNum);
        teachers.put("guoneifangxueNum",guoneifangxueNum);
        teachers.put("peixunNum",peixunNum);
        teachers.put("duboNum",duboNum);
        teachers.put("waijieNum",waijieNum);
        teachers.put("duanlian",duanlian);
        teachers.put("shijiaNum",shijiaNum);
        teachers.put("bingjiaNum",bingjiaNum);
        Map<String,Object> students=new HashMap<String,Object>();
        students.put("num",studNum);
        students.put("sum",studSum);
        result.put("student",students);
        result.put("teacher",teachers);
        return result;
    }

    //根据输入时间获取对应届数学生
    public List<Integer> getYears(String time){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date=sdf.parse(time);
            Calendar cal=Calendar.getInstance();
            cal.setTime(date);
            int year=cal.get(Calendar.YEAR);
            int month=cal.get(Calendar.MONTH);
            List<Integer> years=new ArrayList<Integer>();
            if (month>8){
                for(int i=0;i<4;i++){
                    years.add(year-i);
                }
            }else{
                for(int i=1;i<5;i++){
                    years.add(year-i);
                }
            }
            return years;
        } catch (ParseException e) {
            return null;
        }
    }



    public Map<String,Object> cdAndJbNum(String date)  {
        int cdResult=0;
        int wsjbResult=0;
        int zmjbResult=0;
        //构造晚上加班变量
        String cdsjbz=date+" "+zscd;
        String wsjbsjbz=date+" "+wsjb;
        //表示为今天数据,去track中查询
        Map<String,Object>result=new HashMap<String,Object>();
        Long time1=System.currentTimeMillis();
        System.out.println("date:"+date);
        try {
            if(TimeUtils.isZhoumo(date)){
                if(sdf.format(new Date()).equals(date)){
                    zmjbResult=trackMapper.getZmsbTeacherTotal(date);
                    System.out.println("周末当天:"+(System.currentTimeMillis()-time1));
                }else{
                    zmjbResult=trackPerceptionMapper.getZmsbTeacherTotal(date);
                    System.out.println("周末历史:"+(System.currentTimeMillis()-time1));
                }
                result.put("zmjbNum",zmjbResult);
                result.put("cdNum",0);
                result.put("wsjbNum",0);
            }else{
                if(sdf.format(new Date()).equals(date)){
                    cdResult=trackMapper.getCdTeacherTotal(date,cdsjbz);
                    wsjbResult=trackMapper.getWsjbTeacherTotal(date,wsjbsjbz);
                    System.out.println("非周末当天:"+(System.currentTimeMillis()-time1));
                }else{
                    cdResult=trackPerceptionMapper.getCdTeacherTotal(date,cdsjbz);
                    wsjbResult=trackPerceptionMapper.getWsjbTeacherTotal(date,wsjbsjbz);
                    System.out.println("非周末当天:"+(System.currentTimeMillis()-time1));
                }
                result.put("zmjbNum",0);
                result.put("cdNum",cdResult);
                result.put("wsjbNum",wsjbResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  result;
    }


    //教师请假外派部门列表
    public Map<String, Object> getKqByTeacher(List<String> list, String time) {
        if(null==list||list.isEmpty()||"".equals(list.toString())){
            list=teacherMapper.getAllDw();
        }
        Map<String,Object> result=new LinkedHashMap<String, Object>();
        //获取教师全数据
        List<Map<String,Object>> teachMess=teacherMapper.getMess(list);
        //获取教师请假数据
        List<String> qcByDaate=teacherCheckWorkMapper.getQjByDate(time);
        //获取教师外派数据
        List<String> wpByDaate=teacherCheckWorkMapper.getWpByDate(time);
        //封装
        System.out.println(list);
        for(String dw:list){
            List<Map<String,Object>> dwMess=new ArrayList<Map<String, Object>>();
            for(Map<String,Object>map:teachMess){
                if(dw.equals(map.get("dw"))){
                    //System.out.println(1);
                    Map<String,Object> mapdw=new HashMap<String,Object>();
                    mapdw.put("xm",map.get("xm"));
                    mapdw.put("gh",map.get("gh"));
                    if(qcByDaate.contains(map.get("gh"))){
                        mapdw.put("qj",1);
                    }else{
                        mapdw.put("qj",0);
                    }
                    if(wpByDaate.contains(map.get("gh"))){
                        mapdw.put("wp",1);
                    }else{
                        mapdw.put("wp",0);
                    }
                    dwMess.add(mapdw);
                }
            }
            if(null!=dwMess&&!dwMess.isEmpty()){
                result.put(dw,dwMess);
            }
        }

        return result;
    }


    //离校异常与在校异常获取
    public Map<String,List<String>> getInOrOutSchoolByExcept(String date){
        SimpleDateFormat smd=new SimpleDateFormat("HH:mm:ss");
        Map<String,List<String>>result=new HashMap<String, List<String>>();
        List<String>zx_moring=new ArrayList<String>();
        List<String>lx_moring=new ArrayList<String>();
        List<String>zx_afternoon=new ArrayList<String>();
        List<String>lx_afternoon=new ArrayList<String>();
        List<String>zx_evening=new ArrayList<String>();
        List<String>lx_evening=new ArrayList<String>();
        List<String>zx_midnight=new ArrayList<String>();
        List<String>lx_midnight=new ArrayList<String>();
        //获取1.日期2.人员类型3.时间段感应数据

        //--改天教师上午感应数据
        List<String>ghByMorning=trackPerceptionMapper.getXhgh(date,1,"morning");
        //--改天教师下午感应数据
        List<String>ghByAfternoon=trackPerceptionMapper.getXhgh(date,1,"afternoon");
        //--改天教师晚上感应数据
        List<String>ghByEvening=trackPerceptionMapper.getXhgh(date,1,"evening");
        //--改天教师深夜感应数据
        List<String>ghByMidnight=trackPerceptionMapper.getXhgh(date,1,"midnight");

        //获取当天老师实际考勤
        List<TeacherCheckWorkAll> checks=teacherCheckWorkAllMapper.selectByRq(date);


        System.out.println("morning:"+ghByAfternoon);
        if(checks.isEmpty()){
            return null;
        }
        for(TeacherCheckWorkAll check:checks){
            char[]status=check.getStatuscode().toCharArray();
            //早上离校在校异常判断
            if(String.valueOf(status[0]).equals("1")&&!ghByMorning.contains(check.getYhm())){
                zx_moring.add(check.getYhm());
            }else if(String.valueOf(status[0]).equals("0")&&ghByMorning.contains(check.getYhm())){
                lx_moring.add(check.getYhm());
            }
            //下午离校在校异常判断
            if(String.valueOf(status[1]).equals("1")&&!ghByAfternoon.contains(check.getYhm())){
                zx_afternoon.add(check.getYhm());
            }else if(String.valueOf(status[1]).equals("0")&&ghByAfternoon.contains(check.getYhm())){
                lx_afternoon.add(check.getYhm());
            }
            //晚上离校在校异常判断
            if(String.valueOf(status[2]).equals("1")&&!ghByEvening.contains(check.getYhm())){
                zx_evening.add(check.getYhm());
            }else if(String.valueOf(status[2]).equals("0")&&ghByEvening.contains(check.getYhm())){
                if(!"下班休息".equals(check.getEvening())){
                    lx_evening.add(check.getYhm());
                }
            }

            //午夜离校在校异常判断
            if(String.valueOf(status[3]).equals("1")&&!ghByMidnight.contains(check.getYhm())){
                zx_midnight.add(check.getYhm());
            }else if(String.valueOf(status[3]).equals("0")&&ghByMidnight.contains(check.getYhm())){
                if(!"下班休息".equals(check.getMidnight())){
                    lx_midnight.add(check.getYhm());
                }
            }

        }


        if(date.equals(TimeUtils.getToday())){//如果为日期为当天,即按照现在时间比对,返回对应数据
            if(sdf.format(new Date()).compareTo(Const.TimeSlot.MORNINGEND.getValue())>0){
                result.put("zx_morning",zx_moring);
            }
            if(sdf.format(new Date()).compareTo(Const.TimeSlot.AFTERNOONEND.getValue())>0){
                result.put("zx_afternoon",zx_afternoon);
            }
            if(sdf.format(new Date()).compareTo(Const.TimeSlot.EVENINGEND.getValue())>0){
                result.put("zx_evening",zx_evening);
            }
            result.put("lx_moring",lx_moring);
            result.put("lx_afternoon",lx_afternoon);
            result.put("lx_evening",lx_evening);
            result.put("lx_midnight",lx_midnight);
            return  result;

        }

        result.put("zx_morning",zx_moring);
        result.put("zx_afternoon",zx_afternoon);
        result.put("zx_evening",zx_evening);
        result.put("zx_midnight",zx_midnight);
        result.put("lx_moring",lx_moring);
        result.put("lx_afternoon",lx_afternoon);
        result.put("lx_evening",lx_evening);
        result.put("lx_midnight",lx_midnight);



        return result;
    }


    public static void main(String[] args) {
        char[]status="123456".toCharArray();
        System.out.println(status[1]);
        System.out.println(status[2]);
        System.out.println(status[3]);
        System.out.println(status[4]);
        System.out.println(String.valueOf(status[0]).equals("1"));
    }

}
